package IntelSpam::Filters::HTMLFilter;

use 1.0;

use warnings;
use IntelSpam::Config;
use Data::Dumper;
use Log::Log4perl qw(get_logger);
use HTML::Scrubber;

our $VERSION = '1.0';
our $DEBUG = 1;

our $REGISTER = ['body'];
our $META = {
	'id' => 'html_filter',
	'name' => 'HTML Body Filter',
	'description' => 'Watching email\'s HTML body for bad words',
	'version' => '1.0',
	'weight' => 20
	
};

our $logger = get_logger("IntelSpam::HTMLFilter");

our $CFG = {};
our @bad_words = ();
our $scrubber = HTML::Scrubber->new;

sub new($){
	bless {}, shift;
}

sub filter_body($$$){
	my ($this, $ctx, $entity) = @_;
	find_html($ctx, $entity);
}

sub find_html {
    my ($ctx, $entity, $name) = @_;
    defined($name) or $name = "'anonymous'";
    my $IO;


    # Output the body:
    my @parts = $entity->parts;
    if (@parts) {                     # multipart...
	my $i;
	foreach $i (0 .. $#parts) {       # dump each part...
	    find_html($ctx, $parts[$i], ("$name, part ".(1+$i)));
	}
    }
    else {                            # single part...	

	# Get MIME type, and display accordingly...
	if($entity->head->mime_type =~ /^text\/(\w*)?html$/i){
		$logger->debug("deletected html content --> " . $entity->head->mime_type);
		$body = $entity->body_as_string;
		
		$contents = $scrubber->scrub($body);
		$contents =~ s/[\,\.\;\:]+//g;
		chop $contents;
		$logger->debug("HTMLFilter: Scrubbed contents - $contents");
		my @words = split(/\s+/, $contents); 
		my $wc = $#words + 1;
		if($wc eq 0){
			$wc = 1;
		}
		
		
		my $score = 0;
		$_ = $contents;
		foreach my $bw (@bad_words) {
			chop $bw;
			my $occ=()=/$bw/gi;
			if($occ > 0){
				$occ += $wc * 0.2; #bias
			}			
			$score += $occ;
		}

		
		
		$score = ($score/$wc) * $META->{'weight'};
		$logger->info("filter score --> $score");
		$ctx->getpriv()->{'score'}->insert($META->{'id'}, $META->{'weight'}, $score);
	}
    }
}

sub get_registers($){
	$REGISTER;
}

sub get_meta($){
	$META;
}

sub set_cfg($$){

	my $this = shift;
	$CFG = shift;
	my $conf = $CFG->getcfg($META->{'id'});
	
	if(defined $conf->{'embed'} && $conf->{'embed'} eq 'true'){
		@bad_words = split(/\n/, $conf->{'badwords'});

	}else{
		open FILE, "<",  $conf->{'badwords'} or die $!;
		while(<FILE>){
			chop $_;
			push @bad_words, $_;
		}
	}
	$logger->info("Loaded ". ($#bad_words + 1) ." badword(s)");
	close(FILE);

	$scrubber->default(0);
}

1;

